const fs = require('fs');
function parseJson(json) {
    const map = {};
    for (const key in json.paths) {
        const pathData = json.paths[key];
        for (const k in pathData) {
            const apiData = pathData[k];
            const resData = apiData['responses']['200']['content']
            for (const resKey in resData) {
                const contentData = resData[resKey];
                if (contentData['schema']['$ref']) {
                    map[contentData['schema']['$ref']] = 1;
                }
            }
        }
    }
    return map;
}
function parseReturnCode(json) {
    const map = {};
    for (const key in json.paths) {
        const pathData = json.paths[key];
        for (const k in pathData) {
            const apiData = pathData[k];
            const resData = apiData['responses']
            for (const resKey in resData) {
                map[resKey] = 1;
            }
        }
    }
    return map;
}

function getSchemas(json) {
    const map = {};
    for (const key in json.components.schemas) {
        map[key] = 1;
    }
    return map;
}


const refKey = '$ref'
/** 获取对象所有类型 */
function getSchemasVarType(json) {
    const map = {};
    const schemaData = json.components.schemas
    for (const key in schemaData) {
        const props = schemaData[key].properties;
        for (const p in props) {
            const prop = props[p];
            if (prop.type) {
                map[prop.type] = 1;
                if (prop.format) {
                    map[prop.format] = 1;
                }
            } else if (prop[refKey]) {
                map[refKey] = 1;
            }
        }
    }
    return map;
}

function getTags(json) {
    const map = {};
    json.tags.forEach((item) => {
        map[item.name] = 1;
    });
    return map;
}

function parseBody(json) {
    const map = {};
    for (const key in json.paths) {
        const item = json.paths[key];
        if (item.post) {

            const body = item.post.requestBody;
            if (body) {
                const jsonData = body.content['application/json'];
                for (const k in jsonData.schema) {
                    if(jsonData.schema['type'] == 'array'){
                        console.log(jsonData.schema);
                        
                    }
                    map[k] = 1;
                }
            }
        }
    }
    return map;
}
(function () {
    try {
        const txt = fs.readFileSync('./java_api.json', 'utf8');
        const json = JSON.parse(txt);
        // const map = parseJson(json);
        // let cnt = 0;
        // for (const key in map) {
        //     cnt++;
        // }
        // console.log('key', cnt, map);

        // const returnCodeMap = parseReturnCode(json);
        // console.log('key', Object.keys(returnCodeMap).length, returnCodeMap);

        // const schemaMap = getSchemas(json);
        // console.log('key', Object.keys(schemaMap).length, schemaMap);
        // const schemaTypeMap = getSchemasVarType(json);
        // console.log('key', Object.keys(schemaTypeMap).length, schemaTypeMap);

        // const tagMaps = getTags(json);
        // console.log('key', Object.keys(tagMaps).length, tagMaps);

        const bodyMaps = parseBody(json);

        console.log('key', Object.keys(bodyMaps).length, bodyMaps);
    } catch (error) {
        console.log('err', error);
    }
})();